/**
 * @author gsc
 * @description 用于组件间接的获取state缓存的数据
 */
import $store from '@/store'

export default {

	//获取当前用户登录信息
	userInfo(state) {
		state.userInfo = JSON.parse(localStorage.getItem('userInfo'))
	  return state.userInfo
	},
	currentRoleName(state) {
		if(state.currentRoleName) {
			return state.currentRoleName
		}
		return state.currentRoleName = localStorage.getItem('currentRoleName') ? localStorage.getItem('currentRoleName') :
		state.currentRoleName 
	},
  menus(state) {
    // state.menus = JSON.parse(localStorage.getItem('menus'))
		// return state.menus
		if(state.menus.length > 0) {
			return state.menus
		}
		let localStorageCache
		if(localStorageCache = getLocalStorage('menus')) {
			state.menus = JSON.parse(localStorageCache)
		}
		return state.menus
	},
  systemConfig(state) {
    // state.systemConfig = JSON.parse(localStorage.getItem('systemConfig'))
		// return state.systemConfig
		if(Object.keys(state.systemConfig) > 0) {
			return state.systemConfig
		}
		let localStorageCache
		if(localStorageCache = getLocalStorage('systemConfig')) {
			state.systemConfig = JSON.parse(localStorageCache)
		}
		return state.systemConfig
	},
	// 获取列表页滚动位置
	 listScroll(state) {
		 state.listScroll = localStorage.getItem('listScroll')
		 return state.listScroll
	 },
	filter: (state) => async (beanId) => {
		state.filter = JSON.parse(localStorage.getItem('filter'))
		 if ( state.filter && state.filter[beanId]) {
      return state.filter[beanId]
    }
		await $store.dispatch('recordFilter', {beanId: beanId})
		return state.filter[beanId]
	}

}
const getLocalStorage= ( fieldName) => {
	const localStorageCache = localStorage.getItem(fieldName)
	return localStorageCache
}